<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>device details</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div>
          <el-row :gutter="20">
            <el-col :sm="24" :lg="24">
              <div class="tab-box">
                <h2 class="title-box">{{ deviceDetail ? deviceDetail.name : '--' }}</h2>
                <p class="small-title">Details of the hardware</p>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :sm="24" :lg="24">
              <div class="tab-box">
                <div class="tab-box-line">
                  <span @click="tanLine=1" :class="{active:tanLine==1}">Details</span>
                  <span @click="tanLine=2" :class="{active:tanLine==2}">Warning</span>
                  <span @click="tanLine=3" :class="{active:tanLine==3}">Setting</span>
                  <span @click="tanLine=4" :class="{active:tanLine==4}">Rule</span>
                  <span @click="tanLine=5" :class="{active:tanLine==5}">attribute/History</span>
                </div>
              </div>
            </el-col>
          </el-row>
          <tabDetail v-if="tanLine==1" :deviceDetail="deviceDetail"></tabDetail>
          <tabWarning v-if="tanLine==2"></tabWarning>
          <tabSetting v-if="tanLine==3" @addComponent="addComponent" :componentList="componentList" @componentDelete="componentDelete"></tabSetting>
          <tabRule v-if="tanLine==4" @showRuleJson="showRuleJson" ref="child" :detailId="detailId"></tabRule>
          <tableHistory v-if="tanLine==5" :deviceDetail="deviceDetail"></tableHistory>
        </div>
      </div>
      <div class="cornerComment-in-footer">
        <el-button size="small" @click="closePOpup">cancel</el-button>
        <el-button size="small" type="primary" @click="closePOpup">ok</el-button>
      </div>
    </div>
    <deviceDetail :visible.sync="showJsonvisible"></deviceDetail>
    <editRule :visible.sync="showRulevisible" :ruleId="ruleId" :deviceDetail="deviceDetail"
              @initList="initList" :ruleType="ruleType"></editRule>
    <addDeviceList :visible.sync="showComponentvisible" @getComponent="getComponent"
                   :editooption="editooption"></addDeviceList>
  </div>
</template>

<script>

import tabDetail from "./tabDetail";
import tabWarning from "./tabWarning";
import tabSetting from "./tabSetting";
import deviceDetail from "./deviceDetail";
import tabRule from "./tabRule";
import tableHistory from "./tableHistory";
import editRule from "./editRule";
import {getDevice, updateDevice} from "@/api/system/device";
import addDeviceList from './addDeviceList'

export default {
  props: ['visible', 'detailId'],
  name: "boxDetail",
  components: {addDeviceList, tabDetail, tabWarning, tabSetting, deviceDetail, tabRule, editRule, tableHistory},
  data() {
    return {
      showJsonvisible: false,
      dialogVisible: false,
      showRulevisible: false,
      showComponentvisible: false,
      tanLine: 1,
      componentList: [],
      deviceDetail: null,
      ruleId: 0,
      editooption: null,
      ruleType: ''
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (!this.visible) {
        this.tanLine = 1
      } else {
        this.getDetail()
      }
    }
  },
  created() {
  },
  methods: {
    getDetail() {
      getDevice(this.detailId).then(response => {
        console.log('ressssss==>:', response)
        this.deviceDetail = response.data;
        this.componentList = response.data.componentList;
      });
    },
    addComponent(row) {
      this.editooption = JSON.parse(JSON.stringify(row))
      this.showComponentvisible = true
    },
    showRuleJson(option) {
      this.ruleType = option.type
      this.ruleId = option.id
      this.showRulevisible = true
    },
    getComponent(option, type) {
      console.log('option', option)
      let list_ = JSON.parse(JSON.stringify(this.componentList))
      let form_ = JSON.parse(JSON.stringify(this.deviceDetail))
      if(type!=='delete'){
        if (option.id) {
          let index_ = 0
          for (let index in list_) {
            let item = list_[index]
            if (item.id === option.id) {
              index_ = index
            }
          }
          list_[index_] = option
        } else {
          list_.push(option)
        }
      }else{
        list_.splice(option, 1)
      }

      form_.componentList = list_;
      console.log('提交的数据：', form_)
      updateDevice(form_).then(response => {
        this.$modal.msgSuccess("edit成功");
        this.showComponentvisible = false
        this.getDetail();
      });
    },
    closePOpup() {
      this.$emit('update:visible', false)
    },
    // delete元器件
    componentDelete(index) {
      console.log('deviceDetail:', this.deviceDetail, index)
      this.getComponent(index, 'delete')
    },
    initList() {
      this.$refs.child.getList();
    }
  }
}
</script>

<style scoped>
.Json-list {
  border: 1px solid #eee;
  padding: 10px;
}

.Json-list-line {
  display: flex;
  align-items: center;
}

.w-150 {
  width: 150px;
}

.ml-10 {
  margin-left: 10px;
}

.mr-10 {
  margin-right: 10px;
}

.mb-10 {
  margin-bottom: 10px;
}

#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  /*z-index: 9999;*/
}

.cornerComment-in {
  width: 80%;
  height: 80%;
  background: rgba(245, 246, 250, 1);
  display: flex;
  flex-direction: column;
  border-radius: 5px;
  overflow: hidden;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
  background: #fff;

}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
  background: #fff;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.tab-box {
  background: #fff;
  padding: 10px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .1);
  border: 1px solid #e6ebf5;
  margin-bottom: 10px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  padding-left: 20px;
}

.tab-box-line {
  display: flex;
  align-items: center;
}

.tab-box-line span {
  padding: 10px 5px;
  margin-right: 30px;
  display: block;
  cursor: pointer;
}

.tab-box-line span.active {
  color: #00afff;
  border-bottom: 3px solid #00afff;
}

.title-box {
  margin: 10px 0;
}

.small-title {
  margin: 0;
}
</style>
<!--<h2 class="title-box">Gateway001</h2>-->
<!--<p class="small-title">Details of the hardware</p>-->
